using System;
using System.Collections.Generic;
using UnityEngine;

namespace PKGames.Localization
{
	public class LocationProvider : MonoSingleton<LocationProvider>
	{
		[SerializeField]
		private LocalizationData _localizationData;

		[SerializeField]
		private CurrencyCollection _currencyCollection;

		private string _systemLanguage;

		private string _locationCode;

		private Dictionary<string, Dictionary<string, string>> _localizationMap = new Dictionary<string, Dictionary<string, string>>();

		private IRegionResolver _regionResolver;

		private ISupportedLanguagesResolver _supportedLanguageResolver;

		protected override void Awake()
		{
			base.Awake();
			_regionResolver = RegionResolverFactory.CreateRegionResolver();
			GetRegion();
			ResolveLanguage();
			BuildLocalizationMap();
		}

		public void RegisterSupportedLocation(ISupportedLanguagesResolver supportedLanguageResolver)
		{
			_supportedLanguageResolver = supportedLanguageResolver;
		}

		public void UnregisterSupportedLocation()
		{
			_supportedLanguageResolver = null;
		}

		private void BuildLocalizationMap()
		{
			for (int i = 0; i < _localizationData.SupportedLanguages.Count; i++)
			{
				string key = _localizationData.SupportedLanguages[i];
				_localizationMap[key] = new Dictionary<string, string>();
				foreach (LocalizationEntry datum in _localizationData.Data)
				{
					_localizationMap[key][datum.Key] = datum.Value[i];
				}
			}
		}

		public void ResolveLanguage()
		{
			if (Application.systemLanguage == SystemLanguage.Portuguese)
			{
				_systemLanguage = "pt";
			}
			else if (Application.systemLanguage == SystemLanguage.French)
			{
				_systemLanguage = "fr";
			}
			else if (Application.systemLanguage == SystemLanguage.Spanish)
			{
				_systemLanguage = "es";
			}
			else
			{
				_systemLanguage = "en";
			}
		}

		public string GetLanguage()
		{
			return string.IsNullOrEmpty(_systemLanguage) ? Language.English.ToString() : _systemLanguage;
		}

		public void GetRegion()
		{
			if (!string.IsNullOrEmpty(_locationCode) || _regionResolver == null)
			{
				return;
			}
			_locationCode = _regionResolver.RetrieveDeviceLocationCode();
			if (string.IsNullOrEmpty(_locationCode))
			{
				if (Application.systemLanguage == SystemLanguage.Portuguese)
				{
					_locationCode = "BR";
				}
				else
				{
					_locationCode = "US";
				}
			}
		}

		public string GetLanguageWithFallback()
		{
			if (_supportedLanguageResolver == null)
			{
				throw new SupportedLanguageResolverException("No supported language registered, please make sure that RegisterSupportedLocation was called before calling this method");
			}
			return _supportedLanguageResolver.FilterSupportedLanguage(_systemLanguage);
		}

		public bool StringKeyExists(string language, string key)
		{
			return _localizationMap.ContainsKey(key);
		}

		public string GetString(string language, string key, string defaultKey)
		{
			if (StringKeyExists(language, key))
			{
				return GetString(language, key);
			}
			return GetString(language, defaultKey);
		}

		public string GetString(string language, string key)
		{
			try
			{
				string text = _localizationMap[language][key];
				return text.Replace("\\n", "\n");
			}
			catch (Exception ex)
			{
				UnityEngine.Debug.LogError(base.Tag + "Localization entry: " + key + " error - " + ex.Message);
				return "Localization entry: " + key + " Not Found";
			}
		}

		public string GetLocalizedString(string key)
		{
			try
			{
				string text = _localizationMap[_systemLanguage][key];
				return text.Replace("\\n", "\n");
			}
			catch (Exception ex)
			{
				UnityEngine.Debug.LogError(base.Tag + "Localization entry: " + key + " error - " + ex.Message);
				return "Localization entry: " + key + " Not Found";
			}
		}

		public string GetCurrencyString(string iso4217Code)
		{
			return _currencyCollection.GetCurrencyCode(iso4217Code);
		}
	}
}
